#include <QSettings>

#include "qlstest1.h"
#include "ui_qlstest1.h"
#include "../../AppFramework/dllQLsPluginsManagerAppfwk/qlspluginsmanagerappfwk.h"

static const int MIN_WIDTH = 628;
static const int MIN_HEIGHT = 209;
static const int STACKEDWIDGET_MIN_WIDTH = 618;
static const int STACKEDWIDGET_MIN_HEIGHT = 174;

static const QString g_pluginsDir = "plugins/QCoreSystem/test1";

QLsTest1::QLsTest1(QWidget *parent) :
	QWidget(parent),
	ui(new Ui::QLsTest1),
	m_miniWidth(MIN_WIDTH),
	m_miniHeight(MIN_HEIGHT),
	m_tabWidgetMiniWidth(STACKEDWIDGET_MIN_WIDTH),
	m_tabWidgetMiniHeight(STACKEDWIDGET_MIN_HEIGHT)
{
	ui->setupUi(this);

	/* 整个窗口大小初始化，重设大小，解决 qss样式更改 及 代码初始化 导致的窗口大小嵌入冲突 */
	if(width() < m_miniWidth || height() < m_miniHeight)
		resize(m_miniWidth, m_miniHeight);

	ui->showTabWidget->installEventFilter(this);

	/* 插件管理 */
	ui->showTabWidget->setTabStyle(QLsPluginsManagerAppfwk::QLsTabStyle::WEST);
	ui->showTabWidget->setPluginsDir(g_pluginsDir);
	connect(this, &QLsTest1::sigLoad, ui->showTabWidget, &QLsPluginsManagerAppfwk::slotPluginLoad);
	connect(ui->showTabWidget, &QLsPluginsManagerAppfwk::sigChangeMinimumSize,
			this, &QLsTest1::slotShowTabWidgetChangeMinimumSize);

	QSettings settings(QString("ini/themes/%1.ini").arg(g_pluginsDir), QSettings::IniFormat);
	m_curThemeName = settings.value("cur/theme", "white").toString();
	ui->showTabWidget->setCurThemeName(m_curThemeName);
	QLsThemesManager *themesManager = ui->showTabWidget->themesManager();
	themesManager->LoadTheme(this, m_curThemeName);

	/* 初始化本地插件 */
	ui->showTabWidget->LoadAllPlugin();
}

QLsTest1::~QLsTest1()
{
	delete ui;
}

/* 插件管理器最小尺寸变化的槽函数 */
void QLsTest1::slotShowTabWidgetChangeMinimumSize(QSize size)
{
	/* 最小宽度变化 */
	if(size.width())
	{
		m_miniWidth += size.width();
		m_tabWidgetMiniWidth += size.width();
		setMinimumWidth(m_miniWidth);
	}

	/* 最小高度变化 */
	if(size.height())
	{
		m_miniHeight += size.height();
		m_tabWidgetMiniHeight += size.height();
		setMinimumHeight(m_miniHeight);
	}

	/* BUG:2020-10-09 15:36：√如果当前为最大化状态，修改窗口尺寸会假死 */
	emit sigChangeMinimumSize();
	if(!isMaximized()) resize(qMax(MIN_WIDTH, m_miniWidth), qMax(MIN_HEIGHT, m_miniHeight));
}
